<template>
	<view>
		<header-top :messageInfo="{typeFlag:true,message:titletext}"></header-top>
		<pickercity :messageInfo="{pickerdzshow:pickerdzshow}" @surecity="surecity"></pickercity>
		<view class="mainpadding">
			<view class="mainpadding">
				<view class="strongtext">姓名<text class="bitian">*</text></view>
				<view class="flexleft iteminput">
					<input type="text" placeholder="请输入姓名" v-model="name">
				</view>
				<view class="flexbetween margin_top">
					<view class="tsflex">
						<view class="strongtext">性别<text class="bitian">*</text></view>
						<view class=" sexSelect radius flexcenter" @click="sexShow=true">
							<view>{{sex}}</view>
							<view class="selecticon"></view>
						</view>
						<u-picker :show="sexShow" :columns="columns" @confirm='sexConfirm' @cancel='sexShow = false'>
						</u-picker>
					</view>
					<view class="tsflex">
						<view class="strongtext">生日<text class="bitian">*</text></view>
						<view class="flexcenter radius birthday" @click="calendarShow=true">
							<view v-show="bir==''" style="color: #999;">请选择生日</view>
							<view v-show="bir!=''" class="bir">{{bir}}</view>
							<view class="timeicon"></view>
						</view>
						<u-datetime-picker :show="calendarShow" v-model="birValue" :minDate="-946800000" :maxDate='nowTime'
							mode="date" @confirm='calendarConfirm' @cancel='calendarShow = false'></u-datetime-picker>
					</view>
				</view>
				<view class="strongtext margin_top">手机号<text class="bitian">*</text></view>
				<view class="flexleft iteminput">
					<input type="text" placeholder="请输入手机号" v-model="mobile">
				</view>
				<view class="strongtext margin_top">部门<text class="bitian">*</text></view>
				<view class="flexbetween iteminput" @click="bumenshow = true">
					<input type="text" placeholder="请选择部门" v-model="bumen.name" disabled="">
					<u-icon name="arrow-down" size="22"></u-icon>
				</view>
				<view class="strongtext margin_top">所属省市<text class="bitian">*</text></view>
				<view class="flexbetween iteminput" @click="pickerdzshow=true">
					<input type="text" placeholder="请选择所属省市" disabled="" v-model="address">
					<view class="address"></view>
				</view>
				<view class="strongtext margin_top">微信号</view>
				<view class="flexleft iteminput">
					<input type="text" placeholder="请输入微信号" v-model="wechat_code">
				</view>
				<view class="strongtext margin_top">身份证号</view>
				<view class="flexleft iteminput">
					<input type="text" placeholder="请输入身份证号" v-model="idcard">
				</view>
				<view class="strongtext margin_top">上传身份证</view>
				<view class="bigbox" v-show="uploadimage1">
					<image style="width: 100%; height: 100%;border-radius: 10rpx;" :src="uploadimage1" mode=""></image>
					<view class="deleteIMg" @click="uploadimage1=''"></view>
				</view>
				<view class="flexcolumn bigbox bigboxborder" v-show="!uploadimage1" @click="sfzz">
					<view class="sfsfm"></view>
					<view>点击上传</view>
				</view>
				<view class="bigbox margin_top" v-show="uploadimage2">
					<image style="width: 100%; height: 100%;border-radius: 10rpx;" :src="uploadimage2" mode=""></image>
					<view class="deleteIMg" @click="uploadimage2=''"></view>
				</view>
				<view class="flexcolumn bigbox margin_top bigboxborder" v-show="!uploadimage2" @click="sfzf">
					<view class="sfszm"></view>
					<view>点击上传</view>
				</view>
				<view class="btn addempoly flexcenter" @click="addyg">确定</view>
			</view>
		</view>
		 <u-picker :show="bumenshow" keyName="name" :columns="bumencolumns" @confirm="bmconfirm" @cancel="bumenshow=false"></u-picker>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				// 性别
				sex:"",
				sexShow:false,
				columns: [
					['男', '女']
				],
				// 生日内容
				bir:"",
				calendarShow: false,
				birValue: Number(new Date()),
				nowTime: new Date().getTime(),
				// 上传图片
				uploadimage1:"",
				uploadimage2:"",
				mobile:"",
				name:"",
				listid:"",
				wechat_code:"",
				idcard:"",
				// 部门
				bumenshow:false,
				bumencolumns:[],
				bumen:{
					id:"",
					name:"",
				},
				// 部门结束
				// 省市选择
				pickerdzshow:false,
				province_id:"",
				city_id:"",
				address:"",
				titletext:"添加员工"
			}
		},
		onLoad(options) {
			if(options.id){
				this.listid = options.id
				this.init(options.id)
				this.titletext = "编辑员工"
			}
			this.getbumen()
		},
		methods: {
			getbumen(){
				httpRequest.request('/employeesapi/user/getEmployeesBm', 'GET', {
				}).then(res => {
					let data = res.data
					this.bumencolumns.push(data)
				})
			},
			bmconfirm(e){
				console.log(e)
				this.bumen.id = e.value[0].id
				this.bumen.name = e.value[0].name
				this.bumenshow = false
			},
			init(id){
				httpRequest.request('/employeesapi/user/ygDetail', 'POST', {
					id
				}).then(res => {
					let data = res.data
					this.name = data.username
					this.sex = data.gender==1?"男":"女"
					this.bir = data.birthday
					this.mobile = data.mobile
					this.bumen.id = data.employeesbm.id
					this.bumen.name = data.employeesbm.name
					this.province_id = data.province_id
					this.city_id = data.city_id
					this.uploadimage1 = data.idcard_zimage
					this.uploadimage2 = data.idcard_fimage
					this.address = data.cityinfo
					this.wechat_code = data.wechat_code
					this.idcard=data.idcard
					
				})
			},
			addyg(){
				if(this.name==""){
					httpRequest.toast("请输入员工姓名")
					return
				}
				if(this.sex==""){
					httpRequest.toast("请选择员工性别")
					return
				}
				if(this.bir==""){
					httpRequest.toast("请选择员工生日")
					return
				}
				if(this.mobile==""){
					httpRequest.toast("请输入电话")
					return
				}
				let mobile = this.mobile;
				if (!mobile.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的手机号码");
					return false;
				}
				if(this.bumen.name==""){
					httpRequest.toast("请选择部门")
					return
				}
				if(this.address==""){
					httpRequest.toast("请选择所属省市")
					return
				}
				let url = "/employeesapi/user/addYgInfo"
				if(this.listid){
					url = "/employeesapi/user/editYgInfo"
				}
				httpRequest.request(url, 'POST', {
					username:this.name,
					gender:this.sex=='男'?1:2,
					birthday:this.bir,
					mobile:this.mobile,
					employees_bm_id:this.bumen.id,
					province_id:this.province_id,
					city_id:this.city_id,
					idcard:this.idcard,
					wechat_code:this.wechat_code,
					idcard_zimage:this.uploadimage1,
					idcard_fimage:this.uploadimage2,
					id:this.listid
				}).then(res => {
					httpRequest.toast(res.msg)
					if(res.code==1){
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				})
			},
			// 省市选择
			surecity(e) {
				this.pickerdzshow = false
				this.province_id = e.provenceid
				this.city_id = e.cityid
				this.address = e.provence + "/" + e.city
			},
			// 上传身份证正面
			sfzz(){
				let _this = this;
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						console.log(res)
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(1, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i],1)
								}
							}).catch(err => {
							});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(1, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									// console.log(res[i])
									_this.uploadImageService(res[i],1)
								}
							}).catch(err => {
							});
						}
					},
					fail: function(res) {
						console.log(res)
					}
				});
			},
			// 上传身份证反面
			sfzf(){
				let _this = this;
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						console.log(res)
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(1, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i],2)
								}
							}).catch(err => {
							});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(1, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									// console.log(res[i])
									_this.uploadImageService(res[i],2)
								}
							}).catch(err => {
							});
						}
					},
					fail: function(res) {
						console.log(res)
					}
				});
			},
			uploadImageService(url,upImg) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					if(upImg==1){
						_this.uploadimage1 = image_item;
					}else if(upImg==2){
						_this.uploadimage2= image_item;
					}
				}).catch(err => {
				});
			},
			// 性别选择
			sexConfirm(n) {
				// console.log(n.value[0]);
				this.sex = n.value[0]
				this.sexShow = false
			},
			// 生日选择
			calendarConfirm(e) {
				this.bir = this.date(e.value)
				this.calendarShow = false
			},
			// 日期转换
			date(timer) {
				var date = new Date(timer); //当前时间
				var year = date.getFullYear() //返回指定日期的年份
				var month = repair(date.getMonth() + 1); //月
				var day = repair(date.getDate()); //日
				var curTime = year + "-" + month + "-" + day
				function repair(i) {
					if (i >= 0 && i <= 9) {
						return "0" + i;
					} else {
						return i;
					}
				}
				return curTime
			},
		}
	}
</script>
<style lang="scss" scoped>
	.strongtext {
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: #333333;
	}
	.sexSelect {
		text-align: center;
		padding: 20rpx 0;
		width: 140rpx;
		border:1rpx solid #999999;
	}
	.margin_top{
		margin-top: 20rpx;
	}
	.iteminput{
		padding: 20rpx 30rpx;
		border: 1rpx solid #999999;
		border-radius: 10rpx;
	}
	.iteminput{
		input{
			width: 100%;
		}
	}
	// 上传图片
	.bigbox{
		height: 325rpx;
		width: 100%;
		border-radius: 10rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: #999999;
		position: relative;
	}
	.bigboxborder{
		border: 1rpx solid #999999;
	}
	.selecticon{
		margin-left: 10rpx;
		background-image: url('@/static/imags/upload/select.png');
		background-size: 100% 100%;
	}
	.selecttext{
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: #999999;
	}
	.address{
		width: 50rpx;
		height: 47rpx;
		background-image: url("@/static/imags/upload/address.png");
		background-size: 100% 100%;
	}
	.bitian{
		color: #ff0000;
	}
	.tsflex{
		flex-direction: column;
		display: flex;
		align-items: flex-start;
		justify-content: flex-start;
	}
	.sexinput{
		width: 159rpx;
		padding: 20rpx 0;
		border: 1rpx solid #999999;
		text-align: center;
	}
	.birthday{
		padding: 20rpx 10rpx;
		border: 1rpx solid #999999;
	}
	.addempoly{
		margin-top: 60rpx;
	}
</style>
